# -*- coding: utf-8 -*-
# @Author  : pony.guan
# @Time    : 2021/12/2 9:09
# Description:

import json
import requests


class Station:
    create_container_spec_api = '/gw/wms/container/spec/createOrUpdateContainerSpec'
    create_iwms_station_api = '/station/createStationThroughESS'
    create_put_wall_api = '/gw/station/putWall/createOrUpdatePutWall'
    create_ess_station_api = '/model/updateModelNotSafe'

    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mzc3NTM3MDIsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiKjoqIiwiJCQkJiYmISEhUk9MRV9zdXBlcl9hZG1pbiJdLCJqdGkiOiIxMjQyMDVhMC0xNGIyLTQ2MjgtOGExYy1hNTdlMWUyZGJiMzUiLCJjbGllbnRfaWQiOiJhZG1pbl9jbGllbnQiLCJzY29wZSI6WyJhbGwiXX0.qvkcFJkEm87S-Zz2R342s9EqW16tanAq8M4lnNyMclg"
    }

    def __init__(self, ip='', port='', domain='', ):
        if ip and port:
            self.create_container_spec_url = f'http://{ip}:{port}{self.create_container_spec_api}'
            self.create_iwms_station_url = f'http://{ip}:{port}{self.create_iwms_station_api}'
            self.create_put_wall_url = f'http://{ip}:{port}{self.create_put_wall_api}'
            self.create_ess_station_url = f'http://{ip}:{port}{self.create_ess_station_api}'

        elif domain:
            raise
            # todo 域名会有多个
            # create_iwms_station_url = f'http://{domain}{self.create_iwms_station_api}'
            # create_put_wall_url = f'http://{domain}{self.create_put_wall_api}'
            # create_ess_station_url = f'http://{domain}{self.create_ess_station_api}'

    def create_container_spec(self):
        container_spec_code = 'xsys'
        description = 'xsys'
        data = [{"level": 2, "bay": 2, "length": "", "width": "", "height": "", "subContainerNum": 4, "checkedList": [],
                 "subContainerList": [
                     {"subContainerCode": "LA1", "priority": "", "subContainerStatus": "ENABLE",
                      "subContainerLength": "",
                      "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
                      "subContainerLoadBearing": "",
                      "level": 1, "bay": 1, "subContainerLevel": 1, "subContainerBay": 1, "children": []},
                     {"subContainerCode": "LA2", "priority": "", "subContainerStatus": "ENABLE",
                      "subContainerLength": "",
                      "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
                      "subContainerLoadBearing": "",
                      "level": 1, "bay": 1, "subContainerLevel": 1, "subContainerBay": 2, "children": []},
                     {"subContainerCode": "LB1", "priority": "", "subContainerStatus": "ENABLE",
                      "subContainerLength": "",
                      "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
                      "subContainerLoadBearing": "",
                      "level": 1, "bay": 1, "subContainerLevel": 2, "subContainerBay": 1, "children": []},
                     {"subContainerCode": "LB2", "priority": "", "subContainerStatus": "ENABLE",
                      "subContainerLength": "",
                      "subContainerWidth": "", "subContainerHeight": "", "subContainerVolume": "",
                      "subContainerLoadBearing": "",
                      "level": 1, "bay": 1, "subContainerLevel": 2, "subContainerBay": 2, "children": []}],
                 "location": "LEFT",
                 "label": "左墙", "essContainerType": "", "containerSpecCode": container_spec_code,
                 "description": description,
                 "containerSpecStatus": "ENABLE", "containerSpecType": "STATION_STATIC_PUTWALL"}]

        r = requests.post(url=self.create_container_spec_url, data=json.dumps(data), headers=self.headers)

        assert r.json()['code'] in [0, '0', 'B02008']
        print(f'创建容器规格成功: {container_spec_code}')

        return container_spec_code, description

    def create_iwms_staion(self):
        succeed_station_code_lst = []
        failed_station_code_lst = []
        for i in range(0, 11):
            station_code = f'xsys{i}'
            params = {
                'stationCode': station_code,
                'stationOperationType': 'OUTBOUND',
                'stationPhysicalType': 'ROBOT',
                'useEssData': 'false'
            }
            r = requests.get(url=self.create_iwms_station_url, params=params)
            if int(json.loads(r.text)['code']) == 0:
                succeed_station_code_lst.append(station_code)
            else:
                failed_station_code_lst.append(station_code)

        # 确保所有工作站创建成功
        print('创建失败的工作站')
        print(failed_station_code_lst)
        assert failed_station_code_lst == []
        print('所有工作站创建成功')
        return succeed_station_code_lst

    def create_put_wall(self, succeed_station_code_lst):
        succeed_put_wall_code_lst = []
        failed_put_wall_code_lst = []

        for station_code in succeed_station_code_lst:
            code = station_code
            description1 = description
            tag_id_lst = [code[4:] + '-' + str(i) for i in range(4, 0, -1)]



a = [{"containerSpecCode": "4槽位播种墙", "containerSpecType": "STATION_STATIC_PUTWALL", "description": "4槽位播种墙",
      "containerSpecStatus": "ENABLE", "level": 2, "bay": 1, "length": 0, "width": 0, "height": 0, "volume": 0,
      "subContainerNum": 2, "subContainerList": [
        {"subContainerCode": "LA1", "subContainerLevel": 1, "subContainerBay": 1, "subContainerStatus": "ENABLE",
         "putWallSlotCode": "LA1", "isSinglePutWallSlot": "FALSE", "ksStationTaskRuleId": "651025278772596736",
         "tagId": "106-3", "priority": 2, "location": "LEFT"},
        {"subContainerCode": "LB1", "subContainerLevel": 2, "subContainerBay": 1, "subContainerStatus": "ENABLE",
         "putWallSlotCode": "LB1", "isSinglePutWallSlot": "FALSE", "ksStationTaskRuleId": "651025278772596736",
         "tagId": "106-1", "priority": 1, "location": "LEFT"}], "location": "LEFT", "essContainerType": "",
      "putWallDisplayStyle": "MERGE", "kind": "STATION_STATIC_PUTWALL", "ksStationTaskRuleId": "651025278772596736",
      "code": "XSYS106", "stationCode": "XSYS106", "putWallStatus": "ENABLE"},
     {"containerSpecCode": "4槽位播种墙", "containerSpecType": "STATION_STATIC_PUTWALL", "description": "4槽位播种墙",
      "containerSpecStatus": "ENABLE", "level": 2, "bay": 1, "length": 0, "width": 0, "height": 0, "volume": 0,
      "subContainerNum": 2, "subContainerList": [
         {"subContainerCode": "RA1", "subContainerLevel": 1, "subContainerBay": 1, "subContainerStatus": "ENABLE",
          "putWallSlotCode": "RA1", "isSinglePutWallSlot": "FALSE", "ksStationTaskRuleId": "651025278772596736",
          "tagId": "106-4", "priority": 1, "location": "RIGHT"},
         {"subContainerCode": "RB1", "subContainerLevel": 2, "subContainerBay": 1, "subContainerStatus": "ENABLE",
          "putWallSlotCode": "RB1", "isSinglePutWallSlot": "FALSE", "ksStationTaskRuleId": "651025278772596736",
          "tagId": "106-2", "priority": 2, "location": "RIGHT"}], "location": "RIGHT", "essContainerType": "",
      "putWallDisplayStyle": "MERGE", "kind": "STATION_STATIC_PUTWALL", "ksStationTaskRuleId": "651025278772596736",
      "code": "XSYS106", "stationCode": "XSYS106", "putWallStatus": "ENABLE"}]
